import React from 'react';
import { createHashRouter, Navigate } from 'react-router-dom';
import App from '../App';
import TabLayout from '../layouts/TabLayout';
import MessageList from '../pages/message/List';
import MessageDetail from '../pages/message/Detail';
import MessageCreate from '../pages/message/Create';
import Tutorial from '../pages/tutorial/Index';
import Profile from '../pages/profile/Index';
import RefundPage from '../pages/RefundPage';
import ComplaintPage from '../pages/ComplaintPage';
import ComplaintDetailPage from '../pages/ComplaintDetailPage';
import { useAuth } from '../context/AuthContext.jsx';

// 会员拦截组件：非会员跳转到首页
function RequireVip({ children }) {
  const { auth } = useAuth();
  const isVip = !!auth?.userInfo?.isVip;
  if (!isVip) {
    return <Navigate to="/" replace />;
  }
  return children;
}

const router = createHashRouter([
  {
    path: '/',
    element: <App />,
  },
  {
    path: '/app',
    element: <TabLayout />,
    children: [
      { index: true, element: <Navigate to="/app/message" replace /> },
      {
        path: 'message',
        element: (
          <RequireVip>
            <MessageList />
          </RequireVip>
        ),
      },
      { path: 'tutorial', element: <Tutorial /> },
      { path: 'profile', element: <Profile /> },
    ],
  },
  {
    path: '/message/create',
    element: (
      <RequireVip>
        <MessageCreate />
      </RequireVip>
    ),
  },
  {
    path: '/message/:id',
    element: (
      <RequireVip>
        <MessageDetail />
      </RequireVip>
    ),
  },
  { path: '/refund', element: <RefundPage /> },
  { path: '/complaint', element: <ComplaintPage /> },
  { path: '/complaint/detail', element: <ComplaintDetailPage /> },
  { path: '*', element: <Navigate to="/" replace /> },
]);

export default router;
